{% extends '__base__.html' %}

{% block title %}编辑书籍{% endblock %}

{% block beforehead %}

<script>

var
    action = '{{ action }}',
    redirect = '{{ redirect }}';

function initVue(profile) {
    var vm = new Vue({
        el: '#form-profile',
        data: profile,
        methods: {
            submit: function (event) {
                event.preventDefault();
                startLoading();
                console.log(this.$data);
                postApi(action, this.$data, function (err, r) {
                    if (err) {
                        showError(err);
                    }
                    else {
                        return location.assign(redirect);
                    }
                    stopLoading();
                });
            },
            cancel: function() {
                location.assign(redirect);
            }
        }
    });
}



</script>

{% endblock %}

{% block content %}

    <div class="uk-width-2-3">
        <form id="form-profile" v-on="submit: submit" class="uk-form uk-form-stacked">
            <div class="uk-alert uk-alert-danger uk-hidden"></div>
            <div class="uk-form-row">
                <label class="uk-form-label">书名:</label>
                <div class="uk-form-controls">
                    <input v-model="name" name="name" type="text" placeholder="标题" class="uk-width-1-1">
                </div>
            </div>
            <div class="uk-form-row">
                <label class="uk-form-label">介绍:</label>
                <div class="uk-form-controls">
                    <textarea v-model="intro" rows="16" name="intro" placeholder="内容" class="uk-width-1-1" style="resize:none;"></textarea>
                </div>
            </div>
            <div class="uk-form-row">
                <label class="uk-form-label">图片:</label>
                <div class="uk-form-controls">
                    <textarea v-model="imgUrl" rows="16" name="imgUrl" placeholder="内容" class="imgUrl uk-width-1-1" style="resize:none;" hidden>{{imgUrl}}</textarea>
                </div>
            </div>
            <input type="file" onchange="previewFile()"><br>
            <img src="" height="200">
            <div class="uk-form-row">
                <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-save"></i> 保存</button>
                <button v-on="click: cancel()" type="button" class="uk-button"><i class="uk-icon-times"></i> 取消</button>
            </div>
        </form>

    </div>
<script>
    function previewFile() {
      var preview = document.querySelector('img');
      var file    = document.querySelector('input[type=file]').files[0];
      var imgUrl  = document.querySelector('.imgUrl');
      var reader  = new FileReader();
      reader.onloadend = function () {
        preview.src = reader.result;
        initVue({
        name: '',
        intro: '',
        imgUrl: preview.src.split(',')[1]
        });
        console.log("1"+preview.src.split(',')[1]);
        console.log("2"+preview.src);
      }
      if (file) {
        reader.readAsDataURL(file);
      } else {
        preview.src = "";

      }
    }
</script>
{% endblock %}
